package hva.team2.viavia;

import hva.team2.viavia.sensors.bluetoothChatAdapted.BluetoothChat;
import hva.team2.viavia.sensors.location.GPSLocationListener;
import hva.team2.viavia.sensors.location.GPSTask;

import java.util.Timer;
import java.util.TimerTask;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class Master extends Activity {

    private TimerTask gpsTask = new GPSTask();
    private Timer timer;
    public BluetoothChat btc;

    ImageView logoViaVia;
    ListView actionsList;

    private void showGPSDisabledAlertToUser() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder
                .setMessage(R.string.enable_gps).setCancelable(false)
                .setPositiveButton(R.string.edit_settings,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Intent callGPSSettingIntent = new Intent(
                                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                startActivity(callGPSSettingIntent);
                            }
                        });
        alertDialogBuilder.setNegativeButton(R.string.cancel_gps,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }


    public void setGPS() {
        LocationManager  locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, R.string.gps_enabled,
                    Toast.LENGTH_SHORT).show();
        } else {
            showGPSDisabledAlertToUser();
        }

        LocationListener locListener = new GPSLocationListener();
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locListener);

        timer = new Timer();
        timer.scheduleAtFixedRate(gpsTask, 3000, 3000);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        //setGPS();
        //btc = new BluetoothChat(this);
        //btc.onStart();


        //Intent serverIntent = new Intent(this, DeviceListActivity.class);
        //startActivityForResult(serverIntent, BluetoothChat.REQUEST_CONNECT_DEVICE_INSECURE);


        Intent serverIntent = new Intent(this, MapRountingTest.class);
        startActivity(serverIntent);

     
    }


    @Override
    public synchronized void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Stop the Bluetooth chat services
        //if (btc.mChatService != null)
          // btc.mChatService.stop();
    }

    @Override
    public synchronized void onResume() {
        super.onResume();

//        if (btc.mChatService != null) {
//            if (btc.mChatService.getState() == BluetoothChatService.STATE_NONE) {
 //               btc.mChatService.start();
 //           }
 //       }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       btc.onActivityResult(requestCode,resultCode,data,this);
    }


    /**
     * Created by sempere on 21/07/13.
     */
    public static class Menu {
    }
}
